//
//  UIImageView+oldImageView.m
//  laonianbang
//
//  Created by 勇拓 李 on 17/10/16.
//  Copyright © 2017年 zaiyou. All rights reserved.
//

#import "UIImageView+oldImageView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "oldImage.h"

#define PREFIX @"http://oy6757pec.bkt.clouddn.com/"

@implementation UIImageView (oldImageView)

- (void)showImage:(NSString *)url
{
    if ([url isKindOfClass:[NSNull class]]) {
        self.image = [UIImage imageNamed:@"默认图片"];
        return;
    }
    
    //set tag
    [self setContentMode:UIViewContentModeScaleAspectFill];
    self.clipsToBounds  = YES;
    [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"默认图片"] options:SDWebImageRetryFailed completed:^(UIImage* image,NSError* error,SDImageCacheType cachType, NSURL* iamgeUrl)
     {
         //[[SDImageCache sharedImageCache] storeImage:image forKey:KEY_FOR_IMAGE_AVATAR_CACHE];
     }];
}

- (void)showBanner:(NSString *)url
{
    //set tag
    [self setContentMode:UIViewContentModeScaleAspectFill];
    self.clipsToBounds  = YES;
    [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"banner"] options:SDWebImageRetryFailed completed:^(UIImage* image,NSError* error,SDImageCacheType cachType, NSURL* iamgeUrl)
     {
         //[[SDImageCache sharedImageCache] storeImage:image forKey:KEY_FOR_IMAGE_AVATAR_CACHE];
     }];
}

- (void)showHeadImg:(NSString *)url
{
    //set tag
    [self setContentMode:UIViewContentModeScaleAspectFill];
    self.clipsToBounds  = YES;
    [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"默认头像"] options:SDWebImageRetryFailed completed:^(UIImage* image,NSError* error,SDImageCacheType cachType, NSURL* iamgeUrl)
     {
         //[[SDImageCache sharedImageCache] storeImage:image forKey:KEY_FOR_IMAGE_AVATAR_CACHE];
     }];
}

- (void)showUrlByPrefix:(NSString *)url
{
    if ([url isKindOfClass:[NSNull class]]) {
        self.image = [UIImage imageNamed:@"默认图片"];
        return;
    }
    
    NSString *newUrl = nil;
    
    if ([url containsString:PREFIX]) {
        newUrl = url;
    }
    else
    {
        newUrl = [NSString stringWithFormat:@"%@%@", PREFIX, url];
    }
    
    //set tag
    [self setContentMode:UIViewContentModeScaleAspectFill];
    self.clipsToBounds  = YES;
    [self sd_setImageWithURL:[NSURL URLWithString:newUrl] placeholderImage:[UIImage imageNamed:@"默认图片"] options:SDWebImageRetryFailed completed:^(UIImage* image,NSError* error,SDImageCacheType cachType, NSURL* iamgeUrl)
     {
         //[[SDImageCache sharedImageCache] storeImage:image forKey:KEY_FOR_IMAGE_AVATAR_CACHE];
     }];
}

//- (NSString *) handleImageUrlWithSuffixOSS:(NSString *)imageUrl sizeSuffix:(NSString *)size {
//    
//    if ([imageUrl isEqual:[NSNull null]]) {
//        return nil;
//    }
//    
//    if ([imageUrl length] < [OSS_ROOT_URL length]) {
//        return imageUrl;
//    }
//    
//    if (![[imageUrl substringToIndex:[OSS_ROOT_URL length]] isEqualToString:OSS_ROOT_URL])
//    {
//        return imageUrl;
//    }
//    
//    imageUrl = [imageUrl stringByReplacingCharactersInRange:NSMakeRange(0, [OSS_ROOT_URL length]) withString:OSS_IMG_ROOT_URL];
//    
//    imageUrl = [NSString stringWithFormat:@"%@%@.png", imageUrl, size];
//    return imageUrl;
//    
//}

- (NSString *)getImagePathCompress:(UIImage *)image
{
    return [self getImagePath:[oldImage compressImage:image ratio:1.0]];
}

//照片获取本地路径转换
- (NSString *)getImagePath:(UIImage *)Image {
    NSString *filePath = nil;
    NSData *data = nil;
    
    if (UIImagePNGRepresentation(Image) == nil) {
        data = UIImageJPEGRepresentation(Image, 1.0);
    } else {
        data = UIImagePNGRepresentation(Image);
    }
    
    //图片保存的路径
    //这里将图片放在沙盒的documents文件夹中
    NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"QiNiuCache"];
    
    //文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //把刚刚图片转换的data对象拷贝至沙盒中
    [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *ImagePath = [[NSString alloc] initWithFormat:@"/theFirstImage.png"];
    [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:ImagePath] contents:data attributes:nil];
    
    //得到选择后沙盒中图片的完整路径
    filePath = [[NSString alloc] initWithFormat:@"%@%@", DocumentsPath, ImagePath];
    return filePath;
}

@end
